#include "StdAfx.h"
#include "GtsNode.h"


CGtsNode::CGtsNode(int AmountIn, int AmountOut, double* outs)
{   
	iAmountIn = AmountIn;
	iAmountOut = AmountOut;	
	position=NULL;	
	iState = CRITICAL_STATE; 
	id = ID++;
	initOuts(outs);
	iInFlow=0;
	iOutFlow=0;
	iPower=0;
}

//CGtsNode::CGtsNode()
//{   
//	iAmountIn = 0;
//	iAmountOut = 0;
//	position=NULL;
//}

CGtsNode::~CGtsNode()
{
	delete(position);
	delete[]outs;
}

void CGtsNode::setName(const string& str)
{
	name = str;
}

string CGtsNode::getName() 
{
	return name;
}


CPoint* CGtsNode::getPosition()
{
	return position;
};
	
void CGtsNode::setPosition(CPoint* position)
{
	this->position = position;
}

void CGtsNode::setPower(double iPower)
{
	this->iPower = iPower;
	//correctState();
}

void CGtsNode::setInFlow(double InFlow)
{
	this->iInFlow = InFlow;
	//correctState();
}

void CGtsNode::setOutFlow(double OutFlow)
{
	this->iOutFlow = OutFlow;
	//correctState();
}

double CGtsNode::getPower() const
{
	return iPower;
}


double CGtsNode::getInFlow() const
{
	return iInFlow;
}

double CGtsNode::getOutFlow() const
{
	return iOutFlow;
}

int CGtsNode:: getInAmount() const{
	return iAmountIn;
}

int CGtsNode:: getOutAmount() const{
	return iAmountOut;
}

double* CGtsNode::getOut(){
	return outs;
}

double CGtsNode::getOutAt(int i){
	if(0 <= i && i < iAmountOut){
       return outs[i];  
	} else {
		//TODO throw IndexOutBoundException!
       return -1;
 	}
}

void CGtsNode::setOutAt(int i, double c){
	if(0 <= i && i < iAmountOut){
		outs[i] = c;
	} else {
		//TODO throw IndexBoundOutException!       
 	}
}

void CGtsNode::resetFlows(){
	iInFlow = 0;
	iOutFlow = 0;
}

double CGtsNode::getAmountOut()const{
	double summ = 0;
	double* p = outs;
	for(int i = 0; i < iAmountOut; i++){
		summ+=p[i];
	}
	return summ;
}

const double  CGtsNode::MAX_POWER = 1000001;

void CGtsNode::initOuts(double* initOuts){
	if(initOuts != NULL){
        outs = initOuts;  
	} else {
		outs = new double[iAmountOut];
		for(int i=0; i < iAmountOut; i++){
			outs[i]=1;
		}
	}
}

bool operator == (const CGtsNode& n1, const CGtsNode& n2){
	return n1.getId() == n2.getId();
}

void CGtsNode::setId(int id1)
{
	id=id1;
}


int CGtsNode::getId()const{
	return id;
}

int CGtsNode::ID = 0;